package com.edu.hre.struts.action.recruit;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.actions.DispatchAction;

import com.edu.hre.common.entity.HrePageInfo;
import com.edu.hre.common.utils.BeanFinder;
import com.edu.hre.common.utils.ComboBoxInit;
import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.common.utils.DynamicConfig;
import com.edu.hre.common.utils.ExcelProcessor;
import com.edu.hre.common.utils.RequestUtils;
import com.edu.hre.common.utils.StringConstants;
import com.edu.hre.db.pojo.HreUserRoleScreen;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.sql.recruit.CandidateDAOService;
import com.edu.hre.struts.form.recruit.CandidateForm;
import com.edu.hre.struts.form.recruit.CandidateListForm;

public class CandidateListAction extends DispatchAction {

	private static final Logger logger = Logger.getLogger(CandidateListAction.class);
	private static final BaseService baseService = BeanFinder.getBean(BaseService.BEAN_NAME, BaseService.class);
	private static final CandidateDAOService candidateDAOService = BeanFinder.getBean(CandidateDAOService.BEAN_NAME, CandidateDAOService.class);
	private final String ACTION_NAME = "candidateList";
 
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
		
		// Print source code location.
		CommonUtils.println(CommonUtils.getSourceCodeLocation());
		
		String action = request.getParameter("action");
		if (action != null && "download".equals(action)) {
			
			ExcelProcessor.exportToCandidateList(response);
			return null;
		}
		
		// Get the authorization of the current user for the specified operations. 
		HreUserRoleScreen userRoleScreen = (HreUserRoleScreen) request.getSession().getAttribute(RequestUtils.Attribute.HreUserRoleScreen);

		CandidateListForm candidateListForm = (CandidateListForm) form;
		
		// Check if the operation is DELETE and the current user is authorized to DELETE.
		if (request.getParameter(StringConstants.BTN_DELETE) != null) {
			candidateDAOService.deleteCandidates(candidateListForm);
		}
		
		// Set authorization information for the form in order to determine if the ADD or DELETE button is shown.
		candidateListForm.setCanAdd(userRoleScreen.isCanCreate());
		candidateListForm.setCanDelete(userRoleScreen.isCanDelete());

		// Get the sortBy parameter to determine how the result list is sorted! Then get the list and return.
		String sortBy = RequestUtils.getParameterStr(request, "sort", null);
		if (sortBy != null) { 
			candidateListForm.setAsc(!candidateListForm.isAsc());
			CommonUtils.sortCollection(candidateListForm.getAllCandidates(), candidateListForm.isAsc(), sortBy);
			return mapping.findForward(ACTION_NAME);
		}

		initFormBean(candidateListForm);
		
		// Paging the displaying list.
		int currentPage = RequestUtils.getParameterInt(request, "page", 1);
		int pageSize = CommonUtils.parseInt(DynamicConfig.getConfig(DynamicConfig.PAGE_SIZE), 20);
		List<CandidateForm> listCandidateForm = candidateDAOService.getListCandidateForm(candidateListForm, pageSize, pageSize * (currentPage - 1));
		int countEmp = candidateDAOService.countCandidateForm(candidateListForm);
		
		candidateListForm.setTotalPage(CommonUtils.getTotalPage(countEmp));
		candidateListForm.setAllCandidates(listCandidateForm);

		ArrayList<HrePageInfo> allPages = CommonUtils.getDisplayingPages(currentPage, CommonUtils.getTotalPage(countEmp));
		candidateListForm.setListPages(allPages);
		candidateListForm.setTotalItems(countEmp);

		return mapping.findForward(ACTION_NAME);
	} 
	 
	private void initFormBean(CandidateListForm beanForm) {

		beanForm.setListEducation(ComboBoxInit.getAllEducations(true));
		beanForm.setListLanguage(ComboBoxInit.getAllLanguages(true));
		beanForm.setListLicense(ComboBoxInit.getAllLicenses(true));
		beanForm.setListSkill(ComboBoxInit.getAllSkills(true));
	}
}
